﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveSequence.Suite.Documents;
using LiveSequence.Suite.Domain;
using LiveSequence.Suite.Views;
using LiveSequence.Suite.Interfaces;
using LiveSequence.Suite.Interfaces;

namespace LiveSequence.Suite.Views
{
  public partial class ClassView : BaseView<ProjectDoc>
  {
    private const int ASSEMBLY_INDEX = 1;
    private const int NAMESPACE_INDEX = 2;
    private const int TYPE_INDEX = 3;
    private const int METHOD_INDEX = 4;
    private const int DIAGRAM_INDEX = 9;

    #region Attributes    

    private DocManager _docManager;
    private ProjectDoc _projectDoc;
    #endregion Attributes

    #region Constructor
    public ClassView()
    {
      InitializeComponent();
      _docManager = DocManager.Instance;
      _docManager.ActiveDocumentChanged += new EventHandler(_docManager_ActiveDocumentChanged);
    }

    #endregion Constructor

    #region Properties

    #endregion Properties

    #region Private Methods
    void _docManager_ActiveDocumentChanged(object sender, EventArgs e)
    {
      ProjectDoc projectDoc = _docManager.ActiveDocument as ProjectDoc;
      if(projectDoc != null)
      {
        _projectDoc = projectDoc; 
      }
    }


    #endregion Private Methods

    #region Public Methods

    #endregion Public Methods

    #region Overrides

    #endregion Overrides

    #region Event Handlers

    #endregion Event Handlers

    /// <summary>
    /// Handles the OnUpdateView event of the _projectController control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void _projectController_OnUpdateView(object sender, EventArgs e)
    {
      _treeView.Nodes.Clear();
      if(SelectedDocument != null)
      {
        LSProject project = SelectedDocument.Project;
        populateMethodDiagramTreeView(project);        
      }
    }

    /// <summary>
    /// Populates the method diagram tree view.
    /// </summary>
    /// <param name="project">The project.</param>
    private void populateMethodDiagramTreeView(LSProject project)
    {
      TreeNode _rootNode = new TreeNode("Methods");
      _treeView.Nodes.Add(_rootNode);

      foreach (LSMethodCallDiagram diagram in project.Diagrams)
      {
        TreeNode assemblyNode = insertNode(_rootNode, diagram.MethodData.AssemblyName, ASSEMBLY_INDEX);
        TreeNode nameSpaceNode = insertNode(assemblyNode, diagram.MethodData.NamespaceText, NAMESPACE_INDEX);
        TreeNode typeNode = insertNode(nameSpaceNode, diagram.MethodData.TypeName, TYPE_INDEX);
        TreeNode methodNode = insertNode(typeNode, diagram.MethodData.MethodName, METHOD_INDEX);
        TreeNode diagramNode = new TreeNode(diagram.Title, DIAGRAM_INDEX, DIAGRAM_INDEX);
        methodNode.Nodes.Add(diagramNode);

        diagramNode.Tag = diagram;
      }

    }

    /// <summary>
    /// Inserts the node.
    /// </summary>
    /// <param name="node">The node.</param>
    /// <param name="p">The p.</param>
    /// <param name="iImageIndex">Index of the i image.</param>
    /// <returns></returns>
    private TreeNode insertNode(TreeNode node, string p, int iImageIndex)
    {
      TreeNode result = null;

      foreach (TreeNode child in node.Nodes)
      {
        if (child.Text == p)
        {
          result = child;
          break;
        }
      }
      
      if (result == null)
      {
        result = new TreeNode(p);
        if (iImageIndex != -1)
        {
          result.ImageIndex = iImageIndex;
          result.SelectedImageIndex = iImageIndex;
        }
        node.Nodes.Add(result);
      }
      return result;
    }

    /// <summary>
    /// Handles the NodeMouseDoubleClick event of the _treeView control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeNodeMouseClickEventArgs"/> instance containing the event data.</param>
    private void _treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
      TreeNode selectedNode = e.Node;
      // TOREVIEW
      //LSMethodCallDiagram selectedDiagram = selectedNode.Tag as LSMethodCallDiagram;
      //if(selectedDiagram!=null)
      //{
      //  _projectController.SelectedDiagram = selectedDiagram;
      //}
    }

    /// <summary>
    /// Handles the AfterSelect event of the _treeView control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
    private void _treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
      TreeNode selectedNode = e.Node;
      LSMethodCallDiagram selectedDiagram = selectedNode.Tag as LSMethodCallDiagram;
      if (selectedDiagram != null)
      {
        PropertyViewManager.Instance.SelectedObject = selectedDiagram;
      }     
    }
  }
}
